<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<#assign ignoredColumns = ['createdAt','updatedAt']>
<mapper namespace="${mapper.packageName}.${baseInfo.fileName}">
    <resultMap id="BaseResultMap" type="${tableClass.fullClassName}">
        <#list tableClass.pkFields as field>
        <id property="${field.fieldName}" column="${field.columnName}" jdbcType="${field.jdbcType}"/>
        </#list>
        <#list tableClass.baseFields as field>
        <result property="${field.fieldName}" column="${field.columnName}" jdbcType="${field.jdbcType}"/>
        </#list>
    </resultMap>

    <sql id="Base_Column_List">
        <#list tableClass.allFields as field>${field.columnName}<#sep>,<#if field_index%3==2>${"\n        "}</#if></#list>
    </sql>
    <!-- region: Insert -->
    <insert id="save" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true">
    INSERT INTO ${tableClass.tableName}(<#list tableClass.baseBlobFields as field><#if !ignoredColumns?seq_contains(field.fieldName)><#if field?index != 0>,</#if>${field.columnName}</#if></#list>)
    VALUES(<#list tableClass.baseBlobFields as field><#if !ignoredColumns?seq_contains(field.fieldName)><#if field?index != 0>,</#if>${'#'}{record.${field.fieldName}}</#if></#list>)
    </insert>

    <insert id="saveSelective" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true">
    INSERT INTO ${tableClass.tableName}(
        <trim suffixOverrides=",">
        <#list tableClass.baseBlobFields as field>
        <#if !ignoredColumns?seq_contains(field.fieldName)>
                <#if field.fullTypeName == "java.lang.String">
            <if test="record.${field.fieldName} != null and record.${field.fieldName} != ''">
                ${field.columnName},
            </if>
                <#else >
            <if test="record.${field.fieldName} != null">
                ${field.columnName},
            </if>
                </#if>
        </#if>
        </#list>
        </trim>) VALUES(
         <trim suffixOverrides=",">
        <#list tableClass.baseBlobFields as field>
        <#if !ignoredColumns?seq_contains(field.fieldName)>
                <#if field.fullTypeName == "java.lang.String">
            <if test="record.${field.fieldName} != null and record.${field.fieldName} != ''">
                ${'#'}{record.${field.fieldName}},
            </if>
                <#else >
            <if test="record.${field.fieldName} != null">
                ${'#'}{record.${field.fieldName}},
            </if>
                </#if>
        </#if>
        </#list>
        </trim>)
    </insert>

    <insert id="saveOnDuplicateKey" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true">
     INSERT INTO ${tableClass.tableName}(<#list tableClass.baseBlobFields as field><#if !ignoredColumns?seq_contains(field.fieldName)><#if field?index != 0>,</#if>${field.columnName}</#if></#list>)
    VALUES(<#list tableClass.baseBlobFields as field><#if !ignoredColumns?seq_contains(field.fieldName)><#if field?index != 0>,</#if>${'#'}{record.${field.fieldName}}</#if></#list>)
     ON DUPLICATE KEY UPDATE
     <trim suffixOverrides=",">
        <#list tableClass.baseBlobFields as field>
            <#if field.fullTypeName == "java.lang.String">
        <if test="record.${field.fieldName} != null and record.${field.fieldName} != ''">
            ${field.columnName} = ${'#'}{record.${field.fieldName}},
        </if>
            <#else >
        <if test="record.${field.fieldName} != null">
            ${field.columnName} = ${'#'}{record.${field.fieldName}},
        </if>
            </#if>
        </#list>
     </trim>
    </insert>

    <insert id="batchSave" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true">
     INSERT INTO ${tableClass.tableName}(<#list tableClass.baseBlobFields as field><#if !ignoredColumns?seq_contains(field.fieldName)><#if field?index != 0>,</#if>${field.columnName}</#if></#list>)
    VALUES
    <foreach collection="records" item="item" separator=",">
      (<#list tableClass.baseBlobFields as field><#if !ignoredColumns?seq_contains(field.fieldName)><#if field?index != 0>,</#if>${'#'}{item.${field.fieldName}}</#if></#list>)
    </foreach>
    </insert>
    <!-- endregion: Insert -->

    <!-- region: Update -->
    <update id="update" parameterType="${tableClass.fullClassName}">
        UPDATE ${tableClass.tableName}
        SET <#list tableClass.baseBlobFields as field>
            ${field.columnName} = ${'#'}{record.${field.fieldName}}<#sep>,</#list>
        WHERE id = ${'#'}{record.id}
    </update>

    <update id="updateSelectiveByPk" parameterType="${tableClass.fullClassName}">
        update ${tableClass.tableName}
        <set>
            <#list tableClass.baseBlobFields as field>
                <#if field.fullTypeName == "java.lang.String">
            <if test="record.${field.fieldName} != null and record.${field.fieldName} != ''">
                ${field.columnName} = ${'#'}{record.${field.fieldName}},
            </if>
                <#else >
            <if test="record.${field.fieldName} != null">
                ${field.columnName} = ${'#'}{record.${field.fieldName}},
            </if>
                </#if>
            </#list>
        </set>
        where id = ${'#'}{record.id}
    </update>

    <update id="removeByPk">
         UPDATE ${tableClass.tableName} AS t
         SET t.deleted = 1
         WHERE
         t.id = ${'#'}{pk}
    </update>

    <update id="removeByPkList">
         UPDATE ${tableClass.tableName} AS t
         SET t.deleted = 1
         WHERE
         t.id IN
        <foreach collection="pkList" open="(" separator="," close=")" item="item">
            ${'#'}{item}
        </foreach>
    </update>

    <update id="resetDefaultVal">
         UPDATE ${tableClass.tableName} AS t
         <set>
             <foreach collection="columns" separator="," item="item">
                 ${'$'}{item} = default
             </foreach>
         </set>
         WHERE
         t.id IN
        <foreach collection="pkList" open="(" separator="," close=")" item="item">
            ${'#'}{item}
        </foreach>
    </update>
    <!-- endregion: Update -->

    <!-- region: Deleted -->
    <delete id="deleteByPk">
        DELETE FROM ${tableClass.tableName}
        WHERE
        id = ${'#'}{pk}
    </delete>

    <delete id="deleteByPkList">
        DELETE FROM ${tableClass.tableName}
        WHERE
        id IN
       <foreach collection="pkList" open="(" separator="," close=")" item="item">
           ${'#'}{item}
       </foreach>
    </delete>
    <!-- endregion: Deleted -->

    <!-- region: Select -->
    <select id="get" resultMap="BaseResultMap">
        SELECT ${'<include refid="Base_Column_List"/>'} FROM ${tableClass.tableName}
        WHERE id = ${'#'}{pk}
        <#list tableClass.baseFields as field><#if field.fieldName == 'deleted'>AND deleted = 0</#if></#list>
    </select>

    <select id="list" resultMap="BaseResultMap">
        <#--noinspection CheckEmptyScriptTag-->
        SELECT ${'<include refid="Base_Column_List"/>'} FROM ${tableClass.tableName}
        <where>
            <#list tableClass.allFields as field>
                <#if field.fullTypeName == "java.lang.String">
            <if test="query.${field.fieldName} != null and query.${field.fieldName} != ''">
                AND ${field.columnName} = ${'#'}{query.${field.fieldName}}
            </if>
                <#else >
            <if test="query.${field.fieldName} != null">
                AND ${field.columnName} = ${'#'}{query.${field.fieldName}}
            </if>
                </#if>
            </#list>
        </where>
    </select>
    <select id="listSpecifiedFields" resultMap="BaseResultMap">
        <#--noinspection CheckEmptyScriptTag-->
        SELECT
        <foreach collection="fields" item="item" separator="," >
            ${'$'}{item}
        </foreach>
        FROM ${tableClass.tableName}
        <where>
            <#list tableClass.allFields as field>
                <#if field.fullTypeName == "java.lang.String">
            <if test="query.${field.fieldName} != null and query.${field.fieldName} != ''">
                AND ${field.columnName} = ${'#'}{query.${field.fieldName}}
            </if>
                <#else >
            <if test="query.${field.fieldName} != null">
                AND ${field.columnName} = ${'#'}{query.${field.fieldName}}
            </if>
                </#if>
            </#list>
        </where>
    </select>

    <!-- endregion: Select  -->


</mapper>
